<?php

declare (strict_types=1);

namespace App\Model;

use App\Model\Model;

/**
 * @property int $id
 * @property int $pid
 * @property string $name
 * @property string $shortname
 * @property string $pinyin
 * @property string $longitude
 * @property string $latitude
 * @property int $level
 * @property int $weigh
 * @property string $status
 * @property string $isdeleted
 * @property string $divisionTname
 */
class Areainfo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'areainfo';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'pid' => 'integer', 'level' => 'integer', 'weigh' => 'integer'];

    //    树形结构
    public function child()
    {
        return $this->hasMany(get_class($this), 'id', 'pid')->select(['id', 'pid', 'name']);
    }

    public function children()
    {
        return $this->child()->with('children');
    }

    public function getAll($area_id = 0)
    {
        if ($area_id) {
            $res = $this->where('id', $area_id)->select(['id', 'pid', 'name'])->with('children')->get()->toArray();
            $remp = [];
            unset($res['0']['children']['0']['children']['0']['children']['0']['children']['0']['children']);
            $remp[] = $res['0']['children']['0']['children']['0']['children']['0']['children']['0'];
            unset($res['0']['children']['0']['children']['0']['children']['0']['children']);
            $remp[] = $res['0']['children']['0']['children']['0']['children']['0'];
            unset($res['0']['children']['0']['children']['0']['children']);
            $remp[] = $res['0']['children']['0']['children']['0'];
            unset($res['0']['children']['0']['children']);
            $remp[] = $res['0']['children']['0'];
            unset($res['0']['children']);
            $remp[] = $res['0'];
            $remp[] = [];
            return array_filter($remp);
        } else {
            return [];
        }

    }
}